package com.lemon.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.dispatcher.HttpParameters;
import org.apache.struts2.dispatcher.Parameter;

import com.lemon.domain.Employee;
import com.lemon.domain.Role;
import com.lemon.service.EmployeeService;
import com.lemon.service.RoleService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class RoleAction extends ActionSupport implements ModelDriven<Role> {

	private static final long serialVersionUID = 1L;
	private RoleService roleService = new RoleService();
	private EmployeeService employeeService = new EmployeeService();

	private Role role = new Role();

	/**
	 * 添加一个角色
	 * 
	 * @return
	 */
	public String save() {
		roleService.save(role);
		return SUCCESS;
	}

	/**
	 * 修改角色信息
	 * 
	 * @return
	 */
	public String edit() {
		roleService.update(role);
		return SUCCESS;
	}

	/**
	 * 删除角色
	 * 
	 * @return
	 */
	public String delete() {
		ActionContext context = ActionContext.getContext();
		// 获取要删除的角色的ID
		String param = getParam(context, "id");
		Long id = Long.valueOf(param);
		roleService.delete(id);

		return SUCCESS;
	}

	/**
	 * 跳转到修改页面
	 * 
	 * @return
	 */
	public String toEdit() {
		ActionContext context = ActionContext.getContext();
		// 获取被修改的角色的ID
		String param = getParam(context, "id");
		Long id = Long.valueOf(param);
		// 根据ID查询角色
		Role role = roleService.get(id);
		context.put("role", role);

		return SUCCESS;
	}

	/**
	 * 跳转到列表页面
	 * 
	 * @return
	 */
	public String toList() {
		ActionContext context = ActionContext.getContext();
		List<Role> roles = roleService.list();
		context.put("roles", roles);

		return SUCCESS;
	}

	/**
	 * 跳转到保存页面
	 * 
	 * @return
	 */
	public String toSave() {
		return SUCCESS;
	}

	/**
	 * 跳转到成员管理页面
	 * 
	 * @return
	 */
	public String toMemberManagement() {
		ActionContext context = ActionContext.getContext();
		List<Role> roles = roleService.list();
		context.put("roles", roles);
		List<Employee> employees = employeeService.list();
		context.put("employees", employees);

		return SUCCESS;
	}

	// 角色ID
	private Long roleId;
	// 成员ID数组
	private Long[] members;

	/**
	 * 为员工分配角色
	 * 
	 * @return
	 */
	public String addMember() {
		roleService.addMember(roleId, members);
		return SUCCESS;
	}

	// json数据集合
	private Map<String, Object> json;

	/**
	 * 返回成员json
	 * 
	 * @return
	 */
	public String resultJSON() {
		ActionContext context = ActionContext.getContext();
		// 获取角色ID
		String param = getParam(context, "id");
		Long id = Long.valueOf(param);
		// 查询扮演该角色的员工
		List<Employee> employees = employeeService.findByRoleId(id);
		json = new HashMap<>();
		for (Employee e : employees) {
			json.put(e.getId().toString(), e.getUsername());
		}

		return SUCCESS;
	}

	private String getParam(ActionContext context, String key) {
		HttpParameters parameters = context.getParameters();
		Parameter param = parameters.get(key);
		return param.getValue();
	}

	@Override
	public Role getModel() {
		return role;
	}

	public Long getRoleId() {
		return roleId;
	}

	public void setRoleId(Long roleId) {
		this.roleId = roleId;
	}

	public Long[] getMembers() {
		return members;
	}

	public void setMembers(Long[] members) {
		this.members = members;
	}

	public Map<String, Object> getJson() {
		return json;
	}

	public void setJson(Map<String, Object> json) {
		this.json = json;
	}

}
